package com.youlai.boot.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.annotation.Log;
import com.youlai.boot.common.annotation.RepeatSubmit;
import com.youlai.boot.common.enums.LogModuleEnum;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.core.security.util.SecurityUtils;
import com.youlai.boot.system.model.form.SpotTicketForm;
import com.youlai.boot.system.model.query.SpotTicketQuery;
import com.youlai.boot.system.model.vo.SpotTicketVO;
import com.youlai.boot.system.service.SpotTicketService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 预售票数据审核前端控制层
 *
 * @author 李文兵
 * @since 2025-06-29 17:29
 */
@Tag(name = "预售票数据审核接口")
@RestController
@RequestMapping("/api/v1/spotTickets/audit")
@RequiredArgsConstructor
public class SpotTicketAuditController {

    private final SpotTicketService spotTicketService;

    //@Operation(summary = "预售票数据审核分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('system:spotTicket-audit:query')")
    public PageResult<SpotTicketVO> getSpotTicketPage(SpotTicketQuery queryParams ) {
        if (queryParams.getAuditResult() == null){
            queryParams.setAuditFlag(1);
        }
        IPage<SpotTicketVO> result = spotTicketService.getSpotTicketPage(queryParams);
        return PageResult.success(result);
    }

    //@Operation(summary = "获取预售票审核表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('system:spotTicket-audit:edit')")
    public Result<SpotTicketForm> getSpotTicketForm(
        @Parameter(description = "预售票数据审核ID") @PathVariable Long id
    ) {
        SpotTicketForm formData = spotTicketService.getSpotTicketFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "预售票审核")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('system:spotTicket-audit:edit')")
    @RepeatSubmit(expire = 3)
    @Log(value = "预售票审核", module = LogModuleEnum.OTHER)
    public Result<Void> updateSpotTicket(
            @Parameter(description = "预售票数据审核ID") @PathVariable Long id,
            @RequestBody @Validated SpotTicketForm formData
    ) {
        Long userId = SecurityUtils.getUserId();
        formData.setAuditBy(userId);
        LocalDateTime now = LocalDateTime.now();
        formData.setAuditTime(now);
        boolean result = spotTicketService.updateSpotTicket(id, formData, true);
        return Result.judge(result);
    }

    @Operation(summary = "预售票审核批量退回")
    @PostMapping("/refund/{ids}")
    @PreAuthorize("@ss.hasPerm('system:spotTicket-audit:refund')")
    @RepeatSubmit(expire = 3)
    @Log(value = "预售票审核批量退回", module = LogModuleEnum.OTHER)
    public Result<Void> refundSpotTickets(
        @Parameter(description = "预售票数据审核ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = spotTicketService.refundSpotTickets(ids);
        return Result.judge(result);
    }
    @Operation(summary = "预售票审核批量审核")
    @PostMapping("/audit/{ids}")
    @PreAuthorize("@ss.hasPerm('system:spotTicket-audit:audit')")
    @RepeatSubmit(expire = 3)
    @Log(value = "预售票审核批量审核", module = LogModuleEnum.OTHER)
    public Result<Void> auditSpotTickets(
            @Parameter(description = "预售票数据ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = spotTicketService.auditSpotTickets(ids);
        return Result.judge(result);
    }
}
